﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using PMPUtils.DB;

namespace PMPUtils.Form
{
    public class GridViewOperator
    {
        public static int GetCellID(GridView gridView, String columnName)
        {
            int cellID = 0;

            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                if (gridView.Columns[i].HeaderText.ToUpper().Equals(columnName.ToUpper()))
                {
                    cellID = i;
                    break;
                }
            }

            return cellID;
        }

        public static String GetCellStringValue(GridView gridview, GridViewRow row, String columnName)
        {
            int cellID = GetCellID(gridview, columnName);

            return row.Cells[cellID].Text;
        }

        public static int GetCellIntValue(GridView gridview, GridViewRow row, String columnName)
        {
            int cellID = GetCellID(gridview, columnName);

            if (row.Cells[cellID] == null || row.Cells[cellID].Text.Equals(String.Empty))
            {
                return 0;
            }
            return Convert.ToInt32(row.Cells[cellID].Text);
        }

        public static double GetCellDoubleValue(GridView gridview, GridViewRow row, String columnName)
        {
            int cellID = GetCellID(gridview, columnName);

            if (row.Cells[cellID] == null || row.Cells[cellID].Text.Equals(String.Empty))
            {
                return 0.00;
            }

            return Convert.ToDouble(row.Cells[cellID].Text);
        }

        public static String getHyperLinkCellValue(GridView gridview, GridViewRow row, String columnName)
        {
            String hyperLinkText = String.Empty;
            int cellID = GetCellID(gridview, columnName);

            HyperLink hyp = row.Cells[cellID].Controls[0] as HyperLink;

            if (hyp != null)
            {
                hyperLinkText = hyp.Text;
            }

            return hyperLinkText;
        }

        public static void RefreshProgressBackgroundColor(GridView gridView)
        {

            foreach (GridViewRow row in gridView.Rows)
            {
                String status = GridViewOperator.GetCellStringValue(gridView, row, "progress");
                int statusID = Convert.ToInt32(status.Substring(0, 2));
                int cellID = GridViewOperator.GetCellID(gridView, "progress");

                if (row.Cells[cellID] == null || row.Cells[cellID].Text.Equals(String.Empty) || row.Cells[cellID].Text.Equals("&nbsp;"))
                {
                    continue;
                }

                if (statusID >= 60)
                {
                    row.Cells[cellID].BackColor = Color.LightSlateGray;
                }
                else if (statusID >= 50)
                {
                    row.Cells[cellID].BackColor = Color.Green;
                }
                else if (statusID >= 40)
                {
                    row.Cells[cellID].BackColor = Color.YellowGreen;
                }
                else if (statusID >= 30)
                {
                    row.Cells[cellID].BackColor = Color.CornflowerBlue;
                }
                else if (statusID >= 20)
                {
                    row.Cells[cellID].BackColor = Color.Aqua;
                }
            }
        }

        public static void RefreshJIRAStatusBackgroundColor(GridView gridView)
        {

            foreach (GridViewRow row in gridView.Rows)
            {
                String status = GridViewOperator.GetCellStringValue(gridView, row, "status");
                int cellID = GridViewOperator.GetCellID(gridView, "status");

                if (row.Cells[cellID] == null || row.Cells[cellID].Text.Equals(String.Empty) || row.Cells[cellID].Text.Equals("&nbsp;"))
                {
                    continue;
                }

                if (status.Equals("Complete"))
                {
                    row.Cells[cellID].BackColor = Color.Green;
                }
                else if (status.Equals("Needs Merge") ||
                    status.Equals("Needs Commit") ||
                    status.Equals("Review"))
                {
                    row.Cells[cellID].BackColor = Color.Aqua;
                }
                else if (status.Equals("Reopened") ||
                    status.Equals("NMI"))
                {
                    row.Cells[cellID].BackColor = Color.OrangeRed;
                }
                else if (status.Equals("Hold"))
                {
                    row.Cells[cellID].BackColor = Color.LightSlateGray;
                }
            }
        }

        public static void RefreshBurndownBackgroudColor(GridView gridView, String team, String sprint)
        {

            for (int i = 1; i <= 10; i++)
            {
                String weekday = "Weekday" + Convert.ToString(i).PadLeft(2, '0'); ;

                int weekdayCellID = GetCellID(gridView, weekday);

                foreach (GridViewRow row in gridView.Rows)
                {
                    if (row.Cells[weekdayCellID] == null || row.Cells[weekdayCellID].Text.Equals(String.Empty) || row.Cells[weekdayCellID].Text.Equals("&nbsp;"))
                    {
                        continue;
                    }

                    // No need to deal with initialized value
                    String weekdayRow = gridView.Columns[weekdayCellID].HeaderText.ToUpper();
                    double value = Convert.ToDouble(row.Cells[weekdayCellID].Text);
                    String valueRow = GetCellStringValue(gridView, row, "type").ToUpper();
                    String teamRow = GetCellStringValue(gridView, row, "team").ToUpper();

                    if (weekdayRow.Equals(weekday.ToUpper()))
                    {
                        if (valueRow.Equals("WORKLOAD".ToUpper()) &&
                            teamRow.Equals(team.ToUpper()) ||
                            valueRow.Equals("WORKLOAD_EVM".ToUpper()) &&
                            teamRow.Equals(team.ToUpper()))
                        {

                            String strQueryStatus = "select " +
                                    " case when " + value + " >= " + weekday + "*1.2 then 'RED' when " + value + " >= " + weekday + "*1.1 then 'YELLOW' else 'GREEN' end Status " +
                                    " from Burndown " +
                                    " where team = '" + team + "' and type = 'CAPACITY' and sprint = " + sprint + "";

                            DBOperator dbOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);
                            DataTable dt = dbOperator.Read(strQueryStatus);

                            String status = String.Empty;

                            foreach (DataRow dtRow in dt.Rows)
                            {
                                status = dtRow["status"].ToString();

                                break;
                            }

                            if (status.Equals("RED"))
                            {
                                row.Cells[weekdayCellID].BackColor = Color.Red;
                            }
                            else if (status.Equals("YELLOW"))
                            {
                                row.Cells[weekdayCellID].BackColor = Color.Yellow;
                            }
                            else
                            {
                                row.Cells[weekdayCellID].BackColor = Color.Green;
                            }
                        }
                    }
                }
            }
        }
    }
}
